//
//  ZQTabBar.m
//  柳州微博
//
//  Created by Apple_Lzzy40 on 16/9/26.
//  Copyright © 2016年 Apple_Lzzy40. All rights reserved.
//

#import "ZQTabBar.h"
@interface ZQTabBar()
@property(nonatomic,weak)UIButton *pulsBtn;
@end

@implementation ZQTabBar

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        
        //添加一个Button 到tarbar
        UIButton * plusBtn=[[UIButton alloc]init];
        [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        
        [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
        [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        
        [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        
        //设置图片的尺寸为原图尺寸
        plusBtn.size=plusBtn.currentBackgroundImage.size;
        //plusBtn.centerX=self.width * 0.5;
        // plusBtn.centerY=self.tabBar.height * 0.5;
        
        [plusBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:plusBtn];
        self.pulsBtn=plusBtn;

        
    }
    return self;
}

-(void)btnClick
{
//    NSLog(@"btnClick");
    //3.调用协议方法
    if ([self.touchDelegate respondsToSelector:@selector(TabbarDidClickPlusButton:)]) {
        [self.touchDelegate TabbarDidClickPlusButton:self];
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
   self.pulsBtn .centerX=self.width * 0.5;
   //self.pulsBtn .centerY=self.height * 0.5;
//    NSLog(@"layoutSubviews %@",self.subviews);
    //求按钮的宽度
    CGFloat tabbarBtnWidth=self.width/5;
    int index=0;
    for (int i=0; i<self.subviews.count; i++) {
        //判断是否是   tabbarButton
        UIView *child=self.subviews[i];
        
        Class class=NSClassFromString(@"UITabBarButton");
        
        if ([child isKindOfClass:class]) {
            child.width=tabbarBtnWidth;
            child.x=tabbarBtnWidth * index;
//            NSLog(@"index =%d   child.x =%f",index,child.x);
            index++;
            if (index  ==2) {
                index++;
            }
        }
        
    }
}
@end
